<template>
  <div class="ratingselect">
    <div class="rating-type">
      <span class="block positive" :class="{'active':selectType===2}" @click="select(2)">
        {{desc.all}}
        <span class="count">{{positives.length+negatives.length}}</span>
      </span>
      <span class="block positive" :class="{'active':selectType===0}" @click="select(0)">
        {{desc.positive}}
        <span class="count">{{positives.length}}</span>
      </span>
      <span class="block negative" :class="{'active':selectType===1}" @click="select(1)">
        {{desc.negative}}
        <span class="count">{{negatives.length}}</span>
      </span>
    </div>
    <div class="switch" :class="{'on':onlyContent}" @click="toggleContent">
      <span class="icon-check_circle"></span>
      <span class="text">只看有内容的评价</span>
    </div>
    <slot>
      <div class="rating-wrapper">
        <ul v-show="ratings && ratings.length">
          <li
            v-for="(rating,index) in ratings"
            :key="index"
            class="rating-item"
            v-show="needShow(rating.rateType,rating.text)"
          >
            <div class="user">
              <span class="name">{{rating.username}}</span>
              <img :src="rating.avatar" width="12" height="12" class="avatar" />
            </div>
            <div class="time">{{formatDate(rating.rateTime)}}</div>
            <p class="text">
              <i
                :class="{'icon-thumb_up':rating.rateType===0,'icon-thumb_down':rating.rateType===1}"
              ></i>
              {{rating.text}}
            </p>
          </li>
        </ul>
        <div class="no-rating" v-show="!ratings || !ratings.length">暂无评价</div>
      </div>
    </slot>
  </div>
</template>

<script>
//定义常量
const POSITIVE = 0;
const NEGATIVE = 1;
const ALL = 2;
import { formatDate } from "../../../common/js/date";
export default {
  name: "ratingSelect",
  props: {
    ratings: {
      type: Array,
      default() {
        return [];
      }
    },
    selectType: {
      type: Number,
      default: ALL
    },
    onlyContent: {
      type: Boolean,
      default: false
    },
    desc: {
      type: Object,
      default() {
        return {
          all: "全部",
          positive: "满意",
          negative: "不满意"
        };
      }
    }
  },
  methods: {
    select(type) {
      this.$emit("ratingTypeSelect", type);
    },
    toggleContent() {
      this.$emit("onlyContentSelect");
    },
    needShow(type, text) {
      if (this.onlyContent && !text) return false;

      if (this.selectType === ALL) return true;

      if (this.selectType === type) return true;
    },
    formatDate(time) {
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm");
    }
  },
  computed: {
    positives() {
      return this.ratings.filter(rating => {
        return rating.rateType === POSITIVE;
      });
    },
    negatives() {
      return this.ratings.filter(rating => {
        return rating.rateType === NEGATIVE;
      });
    }
  }
};
</script>

<style lang="stylus" scoped>
.ratingselect
  .rating-type
    padding: 18px 0
    margin: 0 18px
    border-bottom: 1px solid rgba(7, 17, 27, 0.1)
    font-size: 0
    .block
      display: inline-block
      padding: 8px 12px
      border-radius: 2px
      margin-right: 8px
      line-height: 16px
      font-size: 12px
      color: rgb(77, 85, 93)
      &.active
        color: #fff
      .count
        margin-left: 2px
        font-size: 8px
      &.positive
        background-color: rgba(0, 160, 220, 0.2)
        &.active
          background-color: rgb(0, 160, 220)
      &.negative
        background-color: rgba(77, 85, 93, 0.2)
        &.active
          background-color: rgb(7, 17, 27)
  .switch
    padding: 18px 0
    margin: 0 12px
    line-height: 24px
    border-bottom: 1px solid rgba(7, 17, 27, 0.1)
    color: rgb(147, 153, 159)
    font-size: 0
    &.on
      .icon-check_circle
        color: #00c850
    .icon-check_circle
      display: inline-block
      margin-right: 4px
      font-size: 24px
    .text
      display: inline-block
      vertical-align: top
      font-size: 12px
  .rating-wrapper
    padding: 0 18px
    .rating-item
      position: relative
      padding: 16px 0
      border-bottom: 1px solid rgba(7, 17, 27, 0.1)
      .user
        position: absolute
        right: 0
        top: 16px
        font-size: 0
        line-height: 12px
        .name
          display: inline-block
          vertical-align: top
          margin-right: 6px
          font-size: 10px
          color: rgb(147, 153, 159)
        .avatar
          border-radius: 50%
      .time
        margin-bottom: 6px
        line-height: 12px
        font-size: 10px
        color: rgb(147, 153, 159)
      .text
        line-height: 16px
        font-size: 12px
        color: rgb(7, 17, 27)
        .icon-thumb_up, .icon-thumb_down
          margin-right: 4px
          line-height: 16px
          font-size: 12px
        .icon-thumb_up
          color: rgb(0, 160, 220)
        .icon-thumb_down
          color: rgb(147, 153, 159)
    .no-rating
      padding: 16px 0
      font-size: 12px
      color: rgb(147, 153, 159)
</style>